

use "Data/Census w-restrictions.dta", clear

drop if (year == 2010 & fyend <= 630) | (year == 2011 & fyend > 630)

 gen bucket = .
 forvalues x = 1000(500)60000 {
 replace bucket = `x' if ltd_iss_all_other > `x' - 500 & ltd_iss_all_other <= `x' 
 }
 
replace bucket = bucket/1000
 
forvalues y = 2/14 {
gen bucket_`y' = bucket^`y'
}

gen Qdummy = ltd_iss_all_other <= 10000
gen bucket_Q = bucket * Qdummy

forvalues y = 2/14 {
gen bucket`y'_Q = bucket_`y' * Qdummy
}

gen bucket_alt = round((bucket/0.5) - 16)
forvalues q = 1(1)16 {
gen bucket`q' = bucket_alt == `q'
}

drop bucket_alt
keep ltd_iss_all_other id bucket* type_code Qdummy 

save "File 1 for Bootstrap.dta", replace

sort bucket
by bucket: egen count = count(bucket) 
count if ltd_iss_all_other >= 0 
scalar totalcount = r(N)
 
drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000

collapse (mean) count bucket1-bucket16 bucket_2-bucket_14 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

******************************************
*********** COLUMNS 1 & 2 ****************
******************************************
preserve

reg logcount bucket bucket_2-bucket_8 bucket4-bucket13 if ltd_iss_all_other  > 2000 & ltd_iss_all_other  < 40000 

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2)) 

include "Tables/Coefficient Calcs.do"

* Calculation - bottom of section 5.1
di ((actbcount - cfbcount) * 500) / fp
di(((actbcount - cfbcount) * 500) / fp)/(10000 + (((actbcount - cfbcount) * 500) / fp))

 
***************************************************
***  COLUMNS 1 & 2 BOOTSTRAPPED STANDARD ERRORS ***
***************************************************  
  
program define debt_boot_2_1, rclass
 
	use "File 1 for Bootstrap.dta", clear
	
	bsample, cluster(id)

sort bucket
by bucket: egen count = count(bucket) 

count if ltd_iss_all_other >= 0 
scalar totalcount = r(N)
drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000

collapse (mean) count bucket1-bucket16 bucket_2-bucket_14 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

reg logcount bucket bucket_2-bucket_8 bucket4-bucket13 if ltd_iss_all_other  > 2000 & ltd_iss_all_other  < 40000

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2))

include "Tables/Bootstrap Calcs.do"

end

simulate totmanip = ((cfacount - actacount) / totalcount) inranmanip = ((cfacount - actacount) / cfacount)  totmanip_ex = ((actbcount - cfbcount) / totalcount) inranmanip_ex = ((actbcount - cfbcount) / cfacount), reps(500) seed(20316): debt_boot_2_1

include "Tables/SE Calcs.do"

restore

******************************************
*********** COLUMNS 3 & 4 ****************
******************************************

preserve

reg logcount bucket bucket_2-bucket_9 bucket4-bucket13 if ltd_iss_all_other  > 2000 & ltd_iss_all_other  < 40000

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8) + (_b[bucket_9] * 10^9) + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + (_b[bucket_9] * bucket_9) + _b[_cons]) * exp((e(rmse)^2/2)) 

include "Tables/Coefficient Calcs.do"

***************************************************
***  COLUMNS 3 & 4 BOOTSTRAPPED STANDARD ERRORS ***
***************************************************  
  
program define debt_boot_2_2, rclass
 
	use "File 1 for Bootstrap.dta", replace
	
	bsample, cluster(id)

sort bucket
by bucket: egen count = count(bucket) 

count if ltd_iss_all_other >= 0  
scalar totalcount = r(N)
drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000


collapse (mean) count bucket1-bucket16 bucket_2-bucket_14 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

reg logcount bucket bucket_2-bucket_9 bucket4-bucket13 if ltd_iss_all_other  > 2000 & ltd_iss_all_other  < 40000  
scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8) + (_b[bucket_9] * 10^9)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + (_b[bucket_9] * bucket_9) + _b[_cons]) * exp((e(rmse)^2/2))  

include "Tables/Bootstrap Calcs.do"

end

simulate totmanip = ((cfacount - actacount) / totalcount) inranmanip = ((cfacount - actacount) / cfacount)  totmanip_ex = ((actbcount - cfbcount) / totalcount) inranmanip_ex = ((actbcount - cfbcount) / cfacount), reps(500) seed(20316): debt_boot_2_2

include "Tables/SE Calcs.do"


******************************************
*********** COLUMNS 5 & 6 ****************
******************************************

use "Data/Census w-restrictions", clear

drop if (year == 2010 & fyend <= 630) | (year == 2011 & fyend > 630)

gen bucket = .
forvalues x = 1000(250)60000 {
replace bucket = `x' if ltd_iss_all_other > `x' - 250 & ltd_iss_all_other <= `x' 
}
 
replace bucket = bucket/1000
 
forvalues y = 2/14 {
gen bucket_`y' = bucket^`y'
}

gen Qdummy = ltd_iss_all_other <= 10000
gen bucket_Q = bucket * Qdummy

forvalues y = 2/14 {
gen bucket`y'_Q = bucket_`y' * Qdummy
}

gen bucket_alt = round((bucket/0.25) - 32)
forvalues q = 1(1)28 {
gen bucket`q' = bucket_alt == `q'
}

drop bucket_alt
keep ltd_iss_all_other id bucket* type_code Qdummy

save "File 2 for Bootstrap.dta", replace

sort bucket 
by bucket: egen count = count(bucket) 

count if ltd_iss_all_other >= 0 
scalar totalcount = r(N)

drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000

collapse (mean) count bucket1-bucket28 bucket_2-bucket_14 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

reg logcount bucket bucket_2-bucket_8 bucket7-bucket26 if ltd_iss_all_other  > 2000 & ltd_iss_all_other  < 40000 

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8) + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2)) 

include "Tables/Coefficient Calcs.do"

***************************************************
***  COLUMNS 5 & 6 BOOTSTRAPPED STANDARD ERRORS ***
***************************************************  
  
program define debt_boot_2_3, rclass
 
	use "File 2 for Bootstrap.dta", clear
	
	bsample, cluster(id)

sort bucket
by bucket: egen count = count(bucket) 

count if ltd_iss_all_other >= 0 
scalar totalcount = r(N)
drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000

collapse (mean) count bucket1-bucket28 bucket_2-bucket_9 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

reg logcount bucket bucket_2-bucket_8 bucket7-bucket26 if ltd_iss_all_other  > 2000 & ltd_iss_all_other  < 40000 

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2)) 

include "Tables/Bootstrap Calcs.do"

end

simulate totmanip = ((cfacount - actacount) / totalcount) inranmanip = ((cfacount - actacount) / cfacount)  totmanip_ex = ((actbcount - cfbcount) / totalcount) inranmanip_ex = ((actbcount - cfbcount) / cfacount), reps(500) seed(20316): debt_boot_2_3

include "Tables/SE Calcs.do"


******************************************
*********** COLUMNS 7 & 8 ****************
******************************************
restore 

reg logcount bucket bucket_2-bucket_8 bucket4-bucket14 if ltd_iss_all_other > 2000 & ltd_iss_all_other  < 40000 

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2))

* counts from counterfactual *
sum pcount if ltd_iss_all_other > 9500 & ltd_iss_all_other <= 10000 
scalar cfbcount = r(sum)
sum pcount if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 15000
scalar cfacount  = r(sum)

* actual counts  * 
sum count if ltd_iss_all_other > 9500 & ltd_iss_all_other <= 10000 
scalar actbcount = r(sum)
sum count if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 15000 
scalar actacount = r(sum)

* coefficients - Excess Mass
local totmanip_ex = (actbcount - cfbcount) / totalcount 
local inranmanip_ex = (actbcount - cfbcount) / cfacount

* coefficients - Missing Mass
local totmanip = (cfacount - actacount) / totalcount 
local inranmanip = (cfacount - actacount) / cfacount

 
***************************************************
***  COLUMNS 7 & 8 BOOTSTRAPPED STANDARD ERRORS ***
***************************************************  
  
  
program define debt_boot_2_4, rclass
 
	use "File 1 for Bootstrap.dta", clear
	
	bsample, cluster(id)

sort bucket
by bucket: egen count = count(bucket) 

count if ltd_iss_all_other >= 0 
scalar totalcount = r(N)
drop if ltd_iss_all_other > 60000 | ltd_iss_all_other < 1000

collapse (mean) count bucket1-bucket16 bucket_2-bucket_14 Qdummy bucket_Q bucket2_Q bucket3_Q bucket4_Q bucket5_Q bucket6_Q bucket7_Q bucket8_Q bucket9_Q ltd_iss_all_other, by(bucket)

gen logcount = log(count)

reg logcount bucket bucket_2-bucket_8 bucket4-bucket14 if ltd_iss_all_other  > 2000 & ltd_iss_all_other  < 40000

scalar fp = exp((_b[bucket] * 10) + (_b[bucket_2] * 10^2) + (_b[bucket_3] * 10^3) + (_b[bucket_4] * 10^4) + (_b[bucket_5] * 10^5) + (_b[bucket_6] * 10^6) + (_b[bucket_7] * 10^7) + (_b[bucket_8] * 10^8)  + _b[_cons]) * exp((e(rmse)^2/2))
gen pcount = 0
replace pcount = exp((_b[bucket] * bucket) + (_b[bucket_2] * bucket_2) + (_b[bucket_3] * bucket_3) + (_b[bucket_4] * bucket_4) + (_b[bucket_5] * bucket_5) + (_b[bucket_6] * bucket_6) + (_b[bucket_7] * bucket_7) + (_b[bucket_8] * bucket_8) + _b[_cons]) * exp((e(rmse)^2/2)) 


* counts from counterfactual *
sum pcount if ltd_iss_all_other > 9500 & ltd_iss_all_other <= 10000 
scalar cfbcount = r(sum)
sum pcount if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 15000 
scalar cfacount  = r(sum)

* actual counts  * 
sum count if ltd_iss_all_other > 9500 & ltd_iss_all_other <= 10000 
scalar actbcount = r(sum)
sum count if ltd_iss_all_other > 10000 & ltd_iss_all_other <= 15000
scalar actacount = r(sum)

* size of excess mass
return scalar totmanip_ex = (actbcount - cfbcount) / totalcount 
return scalar inranmanip_ex = (actbcount - cfbcount) / cfacount

* size of missing mass
return scalar totmanip = (cfacount - actacount) / totalcount 
return scalar inranmanip = (cfacount - actacount) / cfacount


end

simulate totmanip = ((cfacount - actacount) / totalcount) inranmanip = ((cfacount - actacount) / cfacount)  totmanip_ex = ((actbcount - cfbcount) / totalcount) inranmanip_ex = ((actbcount - cfbcount) / cfacount), reps(500) seed(20316): debt_boot_2_4

erase "File 1 for Bootstrap.dta" 
erase "File 2 for Bootstrap.dta"

include "Tables/SE Calcs.do"

